﻿Imports System.Globalization
Imports System.IO
Partial Public Class Vessel_Size_LCT
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "Vessel Size"
            ddlYear.ClearSelection()
            For i As Integer = 0 To 4
                With ddlYear
                    .Items.Insert(i, Date.Now.AddYears(i - 2).Year)
                    .Items(i).Value = Date.Now.AddYears(i - 2).Year
                End With
            Next
            ddlYear.SelectedIndex = 2
        End If
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub

    Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim service As New _ReportingService.Report

        Dim optWindow As String = "left=0,top=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no"
        Dim Orientation As Boolean = True
        Dim ReportName As String = String.Empty
        Dim reportDataSource As String = String.Empty
        Dim paraName As String = String.Empty
        Dim paraValue As String = String.Empty
        Dim reportType As String = String.Empty
        Dim urlDisplay As String = String.Empty
        Dim dsReport As New DataSet
        Dim dt As DataTable = Create()
        For a As Integer = 0 To 16
            Dim dr As DataRow = dt.NewRow()
            Select Case (a)
                Case 0
                    dr.Item("Size") = ">100000"
                Case 1
                    dr.Item("Size") = ">90000 <100000"
                Case 2
                    dr.Item("Size") = ">80000 <90000"
                Case 3
                    dr.Item("Size") = ">70000 <80000"
                Case 4
                    dr.Item("Size") = ">60000 <70000"
                Case 5
                    dr.Item("Size") = ">50000 <60000"
                Case 6
                    dr.Item("Size") = ">40000 <50000"
                Case 7
                    dr.Item("Size") = ">30000 <40000"
                Case 8
                    dr.Item("Size") = ">20000 <30000"
                Case 9
                    dr.Item("Size") = ">10000 <20000"
                Case 10
                    dr.Item("Size") = ">5000 <10000"
                Case 11
                    dr.Item("Size") = ">3000 <5000"
                Case 12
                    dr.Item("Size") = ">2500 <3000"
                Case 13
                    dr.Item("Size") = ">2000 <2500"
                Case 14
                    dr.Item("Size") = ">1500 <2000"
                Case 15
                    dr.Item("Size") = ">1000 <1500"
                Case 16
                    dr.Item("Size") = "<1000"
            End Select
            dt.Rows.Add(dr)
            dt.AcceptChanges()
        Next
        Dim dtSize As DataTable = clsGet.Get_Booking_Vessel_Size_LCT(ddlYear.SelectedValue, radioPort.SelectedValue)
        For i As Integer = 1 To 12
            Dim dtValue As DataRow() = dtSize.Select("Month = '" & i.ToString("00") & "'")
            If (dtValue.Count > 0) Then

                For a As Integer = 0 To 16
                    Dim CD As Integer = 0
                    Dim CE As Integer = 0
                    For x As Integer = 0 To dtValue.Count - 1
                        Select Case (a)
                            Case 0
                                If (CDbl(dtValue(x).Item("Vehicle_DWT")) > 100000) Then
                                    If (dtValue(x).Item("Vehicle_Destination") = "I") Then
                                        CD += 1
                                    Else
                                        CE += 1
                                    End If
                                End If
                            Case 1
                                If (CDbl(dtValue(x).Item("Vehicle_DWT")) > 90000) And (CDbl(dtValue(x).Item("Vehicle_DWT")) < 100001) Then
                                    If (dtValue(x).Item("Vehicle_Destination") = "I") Then
                                        CD += 1
                                    Else
                                        CE += 1
                                    End If
                                End If
                            Case 2
                                If (CDbl(dtValue(x).Item("Vehicle_DWT")) > 80000) And (CDbl(dtValue(x).Item("Vehicle_DWT")) < 90001) Then
                                    If (dtValue(x).Item("Vehicle_Destination") = "I") Then
                                        CD += 1
                                    Else
                                        CE += 1
                                    End If
                                End If
                            Case 3
                                If (CDbl(dtValue(x).Item("Vehicle_DWT")) > 70000) And (CDbl(dtValue(x).Item("Vehicle_DWT")) < 80001) Then
                                    If (dtValue(x).Item("Vehicle_Destination") = "I") Then
                                        CD += 1
                                    Else
                                        CE += 1
                                    End If
                                End If
                            Case 4
                                If (CDbl(dtValue(x).Item("Vehicle_DWT")) > 60000) And (CDbl(dtValue(x).Item("Vehicle_DWT")) < 70001) Then
                                    If (dtValue(x).Item("Vehicle_Destination") = "I") Then
                                        CD += 1
                                    Else
                                        CE += 1
                                    End If
                                End If
                            Case 5
                                If (CDbl(dtValue(x).Item("Vehicle_DWT")) > 50000) And (CDbl(dtValue(x).Item("Vehicle_DWT")) < 60001) Then
                                    If (dtValue(x).Item("Vehicle_Destination") = "I") Then
                                        CD += 1
                                    Else
                                        CE += 1
                                    End If
                                End If
                            Case 6
                                If (CDbl(dtValue(x).Item("Vehicle_DWT")) > 40000) And (CDbl(dtValue(x).Item("Vehicle_DWT")) < 50001) Then
                                    If (dtValue(x).Item("Vehicle_Destination") = "I") Then
                                        CD += 1
                                    Else
                                        CE += 1
                                    End If
                                End If
                            Case 7
                                If (CDbl(dtValue(x).Item("Vehicle_DWT")) > 30000) And (CDbl(dtValue(x).Item("Vehicle_DWT")) < 40001) Then
                                    If (dtValue(x).Item("Vehicle_Destination") = "I") Then
                                        CD += 1
                                    Else
                                        CE += 1
                                    End If
                                End If
                            Case 8
                                If (CDbl(dtValue(x).Item("Vehicle_DWT")) > 20000) And (CDbl(dtValue(x).Item("Vehicle_DWT")) < 30001) Then
                                    If (dtValue(x).Item("Vehicle_Destination") = "I") Then
                                        CD += 1
                                    Else
                                        CE += 1
                                    End If
                                End If
                            Case 9
                                If (CDbl(dtValue(x).Item("Vehicle_DWT")) > 10000) And (CDbl(dtValue(x).Item("Vehicle_DWT")) < 20001) Then
                                    If (dtValue(x).Item("Vehicle_Destination") = "I") Then
                                        CD += 1
                                    Else
                                        CE += 1
                                    End If
                                End If
                            Case 10
                                If (CDbl(dtValue(x).Item("Vehicle_DWT")) > 5000) And (CDbl(dtValue(x).Item("Vehicle_DWT")) < 10001) Then
                                    If (dtValue(x).Item("Vehicle_Destination") = "I") Then
                                        CD += 1
                                    Else
                                        CE += 1
                                    End If
                                End If
                            Case 11
                                If (CDbl(dtValue(x).Item("Vehicle_DWT")) > 3000) And (CDbl(dtValue(x).Item("Vehicle_DWT")) < 5001) Then
                                    If (dtValue(x).Item("Vehicle_Destination") = "I") Then
                                        CD += 1
                                    Else
                                        CE += 1
                                    End If
                                End If
                            Case 12
                                If (CDbl(dtValue(x).Item("Vehicle_DWT")) > 2500) And (CDbl(dtValue(x).Item("Vehicle_DWT")) < 3001) Then
                                    If (dtValue(x).Item("Vehicle_Destination") = "I") Then
                                        CD += 1
                                    Else
                                        CE += 1
                                    End If
                                End If
                            Case 13
                                If (CDbl(dtValue(x).Item("Vehicle_DWT")) > 2000) And (CDbl(dtValue(x).Item("Vehicle_DWT")) < 2501) Then
                                    If (dtValue(x).Item("Vehicle_Destination") = "I") Then
                                        CD += 1
                                    Else
                                        CE += 1
                                    End If
                                End If
                            Case 14
                                If (CDbl(dtValue(x).Item("Vehicle_DWT")) > 1500) And (CDbl(dtValue(x).Item("Vehicle_DWT")) < 2001) Then
                                    If (dtValue(x).Item("Vehicle_Destination") = "I") Then
                                        CD += 1
                                    Else
                                        CE += 1
                                    End If
                                End If
                            Case 15
                                If (CDbl(dtValue(x).Item("Vehicle_DWT")) > 1000) And (CDbl(dtValue(x).Item("Vehicle_DWT")) < 1501) Then
                                    If (dtValue(x).Item("Vehicle_Destination") = "I") Then
                                        CD += 1
                                    Else
                                        CE += 1
                                    End If
                                End If
                            Case 16
                                If (CDbl(dtValue(x).Item("Vehicle_DWT")) < 1001) Then
                                    If (dtValue(x).Item("Vehicle_Destination") = "I") Then
                                        CD += 1
                                    Else
                                        CE += 1
                                    End If
                                End If
                        End Select
                    Next
                    Select Case (i)
                        Case 1
                            dt.Rows(a).Item("Dmt1") = CD
                            dt.Rows(a).Item("Exp1") = CE
                        Case 2
                            dt.Rows(a).Item("Dmt2") = CD
                            dt.Rows(a).Item("Exp2") = CE
                        Case 3
                            dt.Rows(a).Item("Dmt3") = CD
                            dt.Rows(a).Item("Exp3") = CE
                        Case 4
                            dt.Rows(a).Item("Dmt4") = CD
                            dt.Rows(a).Item("Exp4") = CE
                        Case 5
                            dt.Rows(a).Item("Dmt5") = CD
                            dt.Rows(a).Item("Exp5") = CE
                        Case 6
                            dt.Rows(a).Item("Dmt6") = CD
                            dt.Rows(a).Item("Exp6") = CE
                        Case 7
                            dt.Rows(a).Item("Dmt7") = CD
                            dt.Rows(a).Item("Exp7") = CE
                        Case 8
                            dt.Rows(a).Item("Dmt8") = CD
                            dt.Rows(a).Item("Exp8") = CE
                        Case 9
                            dt.Rows(a).Item("Dmt9") = CD
                            dt.Rows(a).Item("Exp9") = CE
                        Case 10
                            dt.Rows(a).Item("Dmt10") = CD
                            dt.Rows(a).Item("Exp10") = CE
                        Case 11
                            dt.Rows(a).Item("Dmt11") = CD
                            dt.Rows(a).Item("Exp11") = CE
                        Case 12
                            dt.Rows(a).Item("Dmt12") = CD
                            dt.Rows(a).Item("Exp12") = CE
                    End Select
                    dt.AcceptChanges()
                Next

            End If
        Next
        For Each dr As DataRow In dt.Rows
            Dim DSum As Integer = 0
            Dim ESum As Integer = 0
            Try
                DSum += CInt(dr.Item("Dmt1"))
                ESum += CInt(dr.Item("Exp1"))
            Catch ex As Exception

            End Try
            Try
                DSum += CInt(dr.Item("Dmt2"))
                ESum += CInt(dr.Item("Exp2"))
            Catch ex As Exception

            End Try
            Try
                DSum += CInt(dr.Item("Dmt3"))
                ESum += CInt(dr.Item("Exp3"))
            Catch ex As Exception

            End Try
            Try
                DSum += CInt(dr.Item("Dmt4"))
                ESum += CInt(dr.Item("Exp4"))
            Catch ex As Exception

            End Try
            Try
                DSum += CInt(dr.Item("Dmt5"))
                ESum += CInt(dr.Item("Exp5"))
            Catch ex As Exception

            End Try
            Try
                DSum += CInt(dr.Item("Dmt6"))
                ESum += CInt(dr.Item("Exp6"))
            Catch ex As Exception

            End Try
            Try
                DSum += CInt(dr.Item("Dmt7"))
                ESum += CInt(dr.Item("Exp7"))
            Catch ex As Exception

            End Try
            Try
                DSum += CInt(dr.Item("Dmt8"))
                ESum += CInt(dr.Item("Exp8"))
            Catch ex As Exception

            End Try
            Try
                DSum += CInt(dr.Item("Dmt9"))
                ESum += CInt(dr.Item("Exp9"))
            Catch ex As Exception

            End Try
            Try
                DSum += CInt(dr.Item("Dmt10"))
                ESum += CInt(dr.Item("Exp10"))
            Catch ex As Exception

            End Try
            Try
                DSum += CInt(dr.Item("Dmt11"))
                ESum += CInt(dr.Item("Exp11"))
            Catch ex As Exception

            End Try
            Try
                DSum += CInt(dr.Item("Dmt12"))
                ESum += CInt(dr.Item("Exp12"))
            Catch ex As Exception

            End Try
            dr.Item("DmtTotal") = DSum
            dr.Item("ExpTotal") = ESum
            dt.AcceptChanges()
        Next
        dsReport.Tables.Add(dt)
        dsReport.AcceptChanges()

        ReportName = "Vessel_Size_LCT.rdlc"
        ' dsReport = New DataSet
        reportDataSource = "Vessel_Size_Value_Size"
        paraName = "PORT;DateTimeNow"
        If (radioPort.SelectedValue = "B") Then
            paraValue = "BCT" & ";" & DateTime.Now.ToString()
        Else
            paraValue = "LCT" & ";" & DateTime.Now.ToString()
        End If

        Orientation = False
        reportType = radioType.SelectedValue

        urlDisplay = service.CreateReporting(dsReport, reportDataSource, reportType, ReportName, paraName, paraValue, Orientation, "")

        Dim oldFile As String = String.Empty
        oldFile = Server.MapPath(urlDisplay)
        Dim fileInfo As FileInfo = New FileInfo(oldFile)
        Dim reportPath As String = "../WriteReport/"

        Dim newFileName As String = DateTime.Now.ToString("_yyMMdd_HHmmss", New CultureInfo("en-US"))
        newFileName = "Vessel_Size_Report" & newFileName

        If fileInfo.Exists Then
            fileInfo.MoveTo(fileInfo.DirectoryName & "/" & newFileName & fileInfo.Extension)
            urlDisplay = reportPath & newFileName & fileInfo.Extension
        End If

        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "window.open('" + urlDisplay + "','_blank','" + optWindow + "');", True)
    End Sub

    Private Function Create() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("Size", GetType(String))
        dt.Columns.Add("Dmt1", GetType(String))
        dt.Columns.Add("Exp1", GetType(String))
        dt.Columns.Add("Dmt2", GetType(String))
        dt.Columns.Add("Exp2", GetType(String))
        dt.Columns.Add("Dmt3", GetType(String))
        dt.Columns.Add("Exp3", GetType(String))
        dt.Columns.Add("Dmt4", GetType(String))
        dt.Columns.Add("Exp4", GetType(String))
        dt.Columns.Add("Dmt5", GetType(String))
        dt.Columns.Add("Exp5", GetType(String))
        dt.Columns.Add("Dmt6", GetType(String))
        dt.Columns.Add("Exp6", GetType(String))
        dt.Columns.Add("Dmt7", GetType(String))
        dt.Columns.Add("Exp7", GetType(String))
        dt.Columns.Add("Dmt8", GetType(String))
        dt.Columns.Add("Exp8", GetType(String))
        dt.Columns.Add("Dmt9", GetType(String))
        dt.Columns.Add("Exp9", GetType(String))
        dt.Columns.Add("Dmt10", GetType(String))
        dt.Columns.Add("Exp10", GetType(String))
        dt.Columns.Add("Dmt11", GetType(String))
        dt.Columns.Add("Exp11", GetType(String))
        dt.Columns.Add("Dmt12", GetType(String))
        dt.Columns.Add("Exp12", GetType(String))
        dt.Columns.Add("DmtTotal", GetType(String))
        dt.Columns.Add("ExpTotal", GetType(String))
        dt.AcceptChanges()
        Return dt
    End Function

End Class